<template>
	<el-form ref="ruleFormRef" style="max-width: 600px" :model="ruleForm" :rules="rules" label-width="auto"
		class="demo-ruleForm" :size="formSize" status-icon>
		<el-form-item label="节点编号" prop="TypeCode">
			<el-input v-model="ruleForm.TypeCode" />
		</el-form-item>
		<el-form-item label="节点名" prop="TypeName">
			<el-input v-model="ruleForm.TypeName" autocomplete="off" />
		</el-form-item>
		<el-form-item label="节点描述" prop="TypeDesc">
			<el-input v-model="ruleForm.TypeDesc" />
		</el-form-item>
		<el-form-item >
			<el-button type="primary" @click="submitForm(ruleFormRef)">
				保存
			</el-button>
			<el-button @click="resetForm(ruleFormRef)">重置</el-button>
		</el-form-item>
	</el-form>
</template>

<script lang="ts" setup>
	import { reactive, ref } from 'vue'
	import type { ComponentSize, FormInstance, FormRules } from 'element-plus'
	import { ElNotification } from 'element-plus'

	import approvalflow from "../../api/wf_approvalflowapi"
	const emit = defineEmits(['onSave'])
	
	interface RuleForm {
		TypeCode : string
		TypeName : string
		TypeDesc : string
	}
	
	const formSize = ref<ComponentSize>('default')
	const ruleFormRef = ref<FormInstance>()
	const ruleForm = reactive<RuleForm>({
		TypeCode: '',
		TypeName: '',
		TypeDesc: '',
	})
	
	const rules = reactive<FormRules<RuleForm>>({
		TypeName: [
			{ required: true, message: '请输入类型名称', trigger: 'blur' },
			{ min: 2, max: 5, message: '2-5个字符', trigger: 'blur' },
		],
		TypeCode: [
			{
				type: 'string',
				required: true,
				message: '请输入节点类型编号',
				trigger: 'blur',
			},
		]
	
	})
	
	const submitForm = async (formEl : FormInstance | undefined) => {
		if (!formEl) return
		await formEl.validate((valid, fields) => {
			if (valid) {
				approvalflow.post(ruleForm).then((res:any) => {
					if (res.code == 1) {
						ElNotification({
							title: 'Success',
							message: '添加成功',
							type: 'success',
							duration: 800,
							onClose: () => {
								emit("onSave", true)
							}
						})
					} else {
						ElNotification({
							title: 'Error',
							message: '失败',
							type: 'error',
							duration: 800,
						})
					}
				}).catch(err => {
					ElNotification({
						title: 'Error',
						message: '失败',
						type: 'error',
						duration: 800,
					})
				})
			} else {
				console.log('error submit!', fields)
			}
		})
	}
	
	const resetForm = (formEl : FormInstance | undefined) => {
		if (!formEl) return
		formEl.resetFields()
	}
	
</script>


<style>
</style>